/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrameSaveReports.java
 *
 * Created on 26/03/2011, 15:34:35
 */
package user_gui;

import in_out.GeraNumeroDeOrigemETotalReport;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import op.LigacaoResumida;

/**
 *
 * @author Jônatas
 */
public class FrameSaveReports extends javax.swing.JFrame {

    private FramePrincipal parencComponent;
    private List<LigacaoResumida> ligacoes;
    private List<String[]> totalList;

    /** Creates new form FrameSaveReports */
    public FrameSaveReports(FramePrincipal parencComponent, List<LigacaoResumida> ligacoes, List<String[]> totalList) {
        this.parencComponent = parencComponent;
        this.ligacoes = ligacoes;
        this.totalList = totalList;
        parencComponent.setEnabled(false);
        initComponents();
    }

    public void closing() {
        this.parencComponent.setEnabled(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbPDF = new javax.swing.JCheckBox();
        cbXLS = new javax.swing.JCheckBox();
        buttonSave = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtFieldNomeEmpresa = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        dateChooserVencimento = new com.toedter.calendar.JDateChooser();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtFieldOperadora = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Relatórios");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        cbPDF.setSelected(true);
        cbPDF.setText("PDF");

        cbXLS.setText("Excel (XLS)");

        buttonSave.setText("Salvar");
        buttonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveActionPerformed(evt);
            }
        });

        jLabel1.setText("Nome da empresa:");

        jLabel2.setText("Data de Vencimento:");

        jLabel3.setText("Relatórios desejados:");

        jLabel4.setText("Operadora:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbPDF)
                    .addComponent(cbXLS)
                    .addComponent(jLabel3)
                    .addComponent(buttonSave)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4))
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(txtFieldOperadora, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(dateChooserVencimento, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtFieldNomeEmpresa, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtFieldNomeEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel2)
                    .addComponent(dateChooserVencimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(txtFieldOperadora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbPDF)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbXLS)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonSave)
                .addGap(38, 38, 38))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-517)/2, (screenSize.height-209)/2, 517, 209);
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        closing();
    }//GEN-LAST:event_formWindowClosing

    private void buttonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveActionPerformed

        if (!cbPDF.isSelected() && !cbXLS.isSelected()) {
            JOptionPane.showMessageDialog(this, "Pelo menos um tipo de relatório deve ser selecionado.");
            return;
        }

        String nomeEmpresa = txtFieldNomeEmpresa.getText();
        String operadora = txtFieldOperadora.getText();
        Date date = dateChooserVencimento.getDate();

        System.out.println(1);

        Map<String, Object> parametersMap = new HashMap<String, Object>();

        //'

        File file = new File("image", "logo.png");

        System.out.println("Existe: " + file.exists());

        System.out.println(2);
        Image image = null;
        try {
            image = ImageIO.read(file);
        } catch (IOException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, "Falha na leitura do logo");
            return;
        }
        System.out.println(3);
        parametersMap.put("logo", image);
        parametersMap.put("operadora", operadora);
        //

        parametersMap.put("cliente", nomeEmpresa);
        parametersMap.put("data", date);
        System.out.println(4);
        JFileChooser chooser = new JFileChooser();
        chooser.showSaveDialog(this);

        String fileOut = chooser.getSelectedFile().getAbsolutePath();

        GeraNumeroDeOrigemETotalReport report = new GeraNumeroDeOrigemETotalReport(ligacoes, totalList, fileOut, parametersMap, parametersMap);
        report.setGeneratePDF(cbPDF.isSelected());
        report.setGenerateXLS(cbXLS.isSelected());

        ProgressBarFrame frame = new ProgressBarFrame(report, parencComponent, "Gerando Relatórios", "Gerando...");
        frame.setVisible(true);
        frame.execute();

        dispose();
    }//GEN-LAST:event_buttonSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonSave;
    private javax.swing.JCheckBox cbPDF;
    private javax.swing.JCheckBox cbXLS;
    private com.toedter.calendar.JDateChooser dateChooserVencimento;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtFieldNomeEmpresa;
    private javax.swing.JTextField txtFieldOperadora;
    // End of variables declaration//GEN-END:variables
}
